import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import Route from './router'

import ElementUI from 'element-ui';
// 导入全局样式
import './assets/css/global.css'
import 'element-ui/lib/theme-chalk/index.css';

import axios from 'axios';
import TreeTable from 'vue-table-with-tree-grid'
Vue.component('tree-table', TreeTable )

axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
Vue.prototype.$http = axios
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置拦截器
axios.interceptors.request.use(config => {
    nprogress.start()
  const token = sessionStorage.getItem('token')
  if (token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
    config.headers.Authorization = token  //请求头加上token
  }
  return config
})
axios.interceptors.response.use(config=>{
    nprogress.done()
    return config
})

import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme
Vue.use(VueQuillEditor)


Vue.use(VueRouter)
Vue.use(ElementUI)
Vue.prototype.$message = ElementUI.Message

Vue.prototype.$confirm = ElementUI.MessageBox.confirm

Vue.config.productionTip = false

import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts

const router = new VueRouter({
  routes: Route,
  mode: 'history'
})

Vue.filter('dataFormat',function(originval){
    const dt = new Date(originval)
    const y = dt.getFullYear()
    const m = (dt.getMonth()+1+'').padStart(2,'0')
    const d = (dt.getDay()+'').padStart(2,'0')
    const hh = (dt.getHours()+'').padStart(2,'0')
    const mm = (dt.getMinutes()+'').padStart(2,'0')
    const ss = (dt.getSeconds()+'').padStart(2,'0')
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

// 路由跳转的时候判断请求是否携带token，如果没有跳转到登录
router.beforeEach((to, from, next) => {
  // to表示将要访问的路径
  // from表示从哪个路径而来
  // next（）放行   next（'/login'）强制跳转
  if (to.path == '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

new Vue({
  render: h => h(App),
  router: router
}).$mount('#app')
